package cn.pli.bike.ui;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;

import cn.pli.bike.R;


/**
 * Created by pc on 2016/1/25.
 * 常见问题web界面
 */
public class WebViewActivity extends BaseActivityPresenter<WebViewDelegate> implements View.OnClickListener {
    private String url = "";
    private String title = "";
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initConfig();
    }

    /**
     * 初始化
     */
    private void initConfig() {
        if (getIntent().hasExtra("url")) {
            url = getIntent().getExtras().getString("url");
        }

        if (getIntent().hasExtra("title")) {
            title = getIntent().getExtras().getString("title");
        }
        initView();

    }

    private void initView() {
        webView = viewDelegate.get(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl(url);
        viewDelegate.setTitle(title);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (viewDelegate != null) {
                    viewDelegate.get(R.id.imageView).setVisibility(View.VISIBLE);
                }
            }
        });
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }


    protected void onNetworkDisConnected() {

    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.iv_title_back);
    }

    @Override
    protected void onPause() {
        webView.reload();
        super.onPause();
    }

    @Override
    protected void initModel() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
        }
    }
}
